//
//  SDHomeViewModel.m
//  skinDetection
//
//  Created by  liangzhun on 2019/1/2.
//  Copyright © 2019年  liangzhun. All rights reserved.
//

#import "SDHomeViewModel.h"

static SDHomeViewModel *model;
static dispatch_once_t onceToken;

@implementation SDHomeViewModel
+ (instancetype)initModel
{
   
    dispatch_once(&onceToken, ^{
        model = [[SDHomeViewModel alloc] init];
    });
   
    return model;
    
}

/**
 加载数据

 @param block block回调
 */
- (void)loadDataWithBlock:(successBlock)block
{
    
    [SDNetwork getProductListFinish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
            SDHomeProductModel *showModel = [SDHomeProductModel modelObjectWithDictionary:dic];
            self.dataArray = showModel.data;
            NSLog(@"KKKKKKKJJJJJJ%lu",(unsigned long)self.dataArray.count);
            self.heightsArray = [self getheightArray];
            block(YES);
        }else{
            block(NO);
        }
    }];
    
}

/**
 获取行高

 @return 行高数组
 */
- (NSArray *)getheightArray
{
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    
    for (int c = 1; c < self.dataArray.count; c++) {
        
        SDHomeProductData *data = self.dataArray[c];
        
        if (data.list.count > 1) {
            
            NSMutableArray *heghtsarry = [[NSMutableArray alloc] init];
            for (int t = 0; t < data.list.count; t = t+2) {
                
                SDHomeProductList *list1 = data.list[t];
                CGFloat height1 = [SDMethodTool getTextHeightWithText:list1.productName Width:(SDMainScreenWidth - 45)/2.0 Font:Font(13)];
                
                CGFloat height2 = 0;
                if (t+1 < data.list.count) {
                    SDHomeProductList *list2 = data.list[t+1];
                    height2 = [SDMethodTool getTextHeightWithText:list2.productName Width:(SDMainScreenWidth - 45)/2.0 Font:Font(13)];
                }
                
                CGFloat h = height1 > height2? height1:height2;
                [heghtsarry addObject:[NSNumber numberWithFloat:h]];
                
                
            }
            [array addObject:heghtsarry];
            
        }else{
            
            SDHomeProductList *list = data.list[0];
            CGFloat height = [SDMethodTool getTextHeightWithText:list.productName Width:(SDMainScreenWidth - 45)/2.0 Font:Font(13)];
            
            [array addObject:@[[NSNumber numberWithFloat:height]]];
        }
        
    }
    
    return array;
    
}
+ (void)clear
{
    onceToken = 0;
    model = nil;
}
@end
